//
//  WechatPayManager.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/5/11.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "WechatPayManager.h"

@interface WechatPayManager()

@end

@implementation WechatPayManager


//注册微信
+(void)registerWX{
    [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString * _Nonnull log) {
        NSLog(@"WeChatSDK: %@", log);
    }];
    [WXApi registerApp:WX_APP_Key universalLink:WX_APP_UniversalLink];
}

+ (id)shareInstance {
    static WechatPayManager *weChatPayInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        weChatPayInstance = [[WechatPayManager alloc] init];
    });
    return weChatPayInstance;
}

+ (BOOL)handleOpenUrl:(NSURL *)url {
    return [WXApi handleOpenURL:url delegate:[WechatPayManager shareInstance]];
}

+ (void)hangleWechatPayWith:(PayReq *)req {
    [WXApi sendReq:req completion:^(BOOL success) {
        if (success) {
            NSLog(@"拉起微信支付成功");
        } else {
            NSLog(@"拉起微信支付异常");
        }
    }];
}

+(void)handleWechatPayWithPayDic:(NSDictionary *)payDic{
    PayReq *req = [[PayReq alloc] init];
    req.nonceStr = payDic[@"noncestr"];
    req.package = payDic[@"package"];
    req.partnerId = payDic[@"partnerid"];
    req.sign = payDic[@"sign"];
    req.timeStamp = (UInt32)[payDic[@"timestamp"] integerValue];
    req.prepayId = payDic[@"prepayid"];
    [WXApi sendReq:req completion:^(BOOL success) {
        if (success) {
            NSLog(@"拉起微信支付成功");
        } else {
             NSLog(@"微信支付异常");
            [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotification" object:nil userInfo:@{@"payResult":@"微信支付异常",@"payStatus":@(1)}];
        }
    }];
}

#pragma mark - 微信支付回调
- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[PayResp class]]) {
        PayResp *response = (PayResp*)resp;
        switch (response.errCode) {
            case WXSuccess: {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotificationSuccess" object:nil userInfo:@{@"payResult":@"支付成功",@"payStatus":@(0)}];
            }
                break;
            case WXErrCodeCommon: {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotification" object:nil userInfo:@{@"payResult":@"微信支付异常",@"payStatus":@(1)}];
            }
                break;
            case WXErrCodeUserCancel: {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotification" object:nil userInfo:@{@"payResult":@"用户取消支付",@"payStatus":@(2)}];
            }
                break;
            case WXErrCodeSentFail: {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotification" object:nil userInfo:@{@"payResult":@"发送支付信息失败",@"payStatus":@(3)}];
            }
                break;
            case WXErrCodeAuthDeny: {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotification" object:nil userInfo:@{@"payResult":@"微信授权失败",@"payStatus":@(4)}];
            }
                break;
            case WXErrCodeUnsupport: {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatPayResultNotification" object:nil userInfo:@{@"payResult":@"当前微信版本暂不支持",@"payStatus":@(5)}];
            }
                break;
            default: {
                break;
            }
        }
    }
}





@end





